#!/usr/bin/env python
# coding:utf-8


from pandas_02_Viewing_Data import *

print '-----------------------------------------------------------------------------'
"""
                   A         B         C         D
2017-01-01  0.406287  1.158293  0.971685  0.405280
2017-01-02 -0.527079 -1.370936  0.535477 -1.087935
2017-01-03 -0.853528 -0.101003 -1.815336  1.280109
2017-01-04  0.432196 -1.274989 -2.872778  0.932454
2017-01-05  1.193823 -0.469238  0.100933  0.068175
2017-01-06  1.125678 -2.295941  1.071525  0.859002
"""
print '-----------------------------------------------------------------------------'
# Getting

# Selecting a single column, which yields a Series, equivalent to df.A:
print df.get('A')
"""
2017-01-01    0.406287
2017-01-02   -0.527079
2017-01-03   -0.853528
2017-01-04    0.432196
2017-01-05    1.193823
2017-01-06    1.125678
Freq: D, Name: A, dtype: float64
"""
# Selecting via [], which slices the rows.
print df[0:3]
"""
                   A         B         C         D
2017-01-01  0.406287  1.158293  0.971685  0.405280
2017-01-02 -0.527079 -1.370936  0.535477 -1.087935
2017-01-03 -0.853528 -0.101003 -1.815336  1.280109
"""
print '-----------------------------------------------------------------------------'
# Selection by Label

# For getting a cross section using a label:
print df.loc[dates[0]]
"""
A    0.406287
B    1.158293
C    0.971685
D    0.405280
Name: 2017-01-01 00:00:00, dtype: float64
"""
# Selecting on a multi-axis by label:
print df.loc[:, ['A', 'B']]
"""
                   A         B
2017-01-01  0.406287  1.158293
2017-01-02 -0.527079 -1.370936
2017-01-03 -0.853528 -0.101003
2017-01-04  0.432196 -1.274989
2017-01-05  1.193823 -0.469238
2017-01-06  1.125678 -2.295941
"""
# Showing label slicing, both endpoints are included:
print df.loc['20170102':'20170104', ['A', 'B']]
"""
                   A         B
2017-01-02 -0.527079 -1.370936
2017-01-03 -0.853528 -0.101003
2017-01-04  0.432196 -1.274989
"""
# Reduction in the dimensions of the returned object:
print df.loc['20170102', ['A', 'B']]
"""
A   -0.527079
B   -1.370936
Name: 2017-01-02 00:00:00, dtype: float64
"""
# For getting a scalar value:
print df.loc[dates[0], 'A']
"""
0.406286816852
"""
# For getting fast access to a scalar (equivalent to the prior method):
print df.at[dates[0], 'A']
"""
0.406286816852
"""
print '-----------------------------------------------------------------------------'
# Selection by Position

# Select via the position of the passed integers:
print df.iloc[0]
"""
A    0.406287
B    1.158293
C    0.971685
D    0.405280
Name: 2017-01-01 00:00:00, dtype: float64
"""
# By integer slices, acting similar to numpy/python:
print df.iloc[3:5, 0:2]
"""
                   A         B
2017-01-04  0.432196 -1.274989
2017-01-05  1.193823 -0.469238
"""
# For slicing rows explicitly:
print df.iloc[1:3, :]
"""
                   A         B         C         D
2017-01-02 -0.527079 -1.370936  0.535477 -1.087935
2017-01-03 -0.853528 -0.101003 -1.815336  1.280109
"""
# For slicing columns explicitly:
print df.iloc[:, 1:3]
"""
                   B         C
2017-01-01  1.158293  0.971685
2017-01-02 -1.370936  0.535477
2017-01-03 -0.101003 -1.815336
2017-01-04 -1.274989 -2.872778
2017-01-05 -0.469238  0.100933
2017-01-06 -2.295941  1.071525
"""
# By lists of integer position locations, similar to the numpy/python style:
print df.iloc[[1, 2, 4], [0, 2]]
"""
                   A         C
2017-01-02 -0.527079  0.535477
2017-01-03 -0.853528 -1.815336
2017-01-05  1.193823  0.100933
"""
# For getting a value explicitly:
print df.iloc[0, 0]
"""
0.406286816852
"""
# For getting fast access to a scalar (equivalent to the prior method):
print df.iat[0, 0]
"""
0.406286816852
"""
print '-----------------------------------------------------------------------------'
print '-----------------------------------------------------------------------------'
# Boolean Indexing

# (随机产生的数值发生改变，与当前文件之前的代码部分不同)
"""
                   A         B         C         D
2017-01-01  1.140921 -0.697784 -0.285186  1.103433
2017-01-02 -0.543076 -1.840990  1.192200 -0.595520
2017-01-03  1.444103  0.913971  0.074225  0.879305
2017-01-04 -0.153612  0.200743 -0.454564 -0.918784
2017-01-05  0.598518  1.316947 -1.699802  0.441587
2017-01-06  0.464981  0.073928  1.845733  1.646335
"""
# Using a single column’s values to select data.
print df[df.A > 0]
"""
                   A         B         C         D
2017-01-01  1.140921 -0.697784 -0.285186  1.103433
2017-01-03  1.444103  0.913971  0.074225  0.879305
2017-01-05  0.598518  1.316947 -1.699802  0.441587
2017-01-06  0.464981  0.073928  1.845733  1.646335
"""
# Selecting values from a DataFrame where a boolean condition is met.
print df[df > 0]
"""
                   A         B         C         D
2017-01-01  1.140921       NaN       NaN  1.103433
2017-01-02       NaN       NaN  1.192200       NaN
2017-01-03  1.444103  0.913971  0.074225  0.879305
2017-01-04       NaN  0.200743       NaN       NaN
2017-01-05  0.598518  1.316947       NaN  0.441587
2017-01-06  0.464981  0.073928  1.845733  1.646335
"""
# Using the isin() method for filtering:
df2 = df.copy()
df2['E'] = ['one', 'two', 'three', 'four', 'five', 'six']
print df2
"""
                   A         B         C         D      E
2017-01-01  1.140921 -0.697784 -0.285186  1.103433    one
2017-01-02 -0.543076 -1.840990  1.192200 -0.595520    two
2017-01-03  1.444103  0.913971  0.074225  0.879305  three
2017-01-04 -0.153612  0.200743 -0.454564 -0.918784   four
2017-01-05  0.598518  1.316947 -1.699802  0.441587   five
2017-01-06  0.464981  0.073928  1.845733  1.646335    six
"""
print df2[df2['E'].isin(['one', 'three'])]
"""
                   A         B         C         D      E
2017-01-01  1.140921 -0.697784 -0.285186  1.103433    one
2017-01-03  1.444103  0.913971  0.074225  0.879305  three
"""


# (随机产生的数值发生改变，与当前文件之前的代码部分不同)

# Using the isin() method for filtering:
df3 = df2.T
print df3
"""
  2017-01-01 2017-01-02 2017-01-03 2017-01-04 2017-01-05 2017-01-06
A  -0.714331   -1.10709    1.06639    1.84668   0.345292  -0.446231
B   0.721557    1.11256  -0.239466   0.232574   -1.58888    2.26415
C   0.227198  -0.502323  -0.783091   0.607604  -0.628465   0.236746
D    1.12765   0.216901   0.985243    1.75519   -1.52153     1.1189
E        one        two      three       four       five        six
"""
print df3[df3['2017-01-01'].isin(['one', 'three'])]
"""
  2017-01-01 2017-01-02 2017-01-03 2017-01-04 2017-01-05 2017-01-06
E        one        two      three       four       five        six
"""
print '-----------------------------------------------------------------------------'
# Setting

# (随机产生的数值发生改变，与当前文件之前的代码部分不同)

# Setting a new column automatically aligns the data by the indexes.
s1 = pd.Series(
    [1, 2, 3, 4, 5, 6],
    index=pd.date_range('20170101', periods=6)
)
print s1
"""
2017-01-01    1
2017-01-02    2
2017-01-03    3
2017-01-04    4
2017-01-05    5
2017-01-06    6
Freq: D, dtype: int64
"""
df['F'] = s1
print df
"""
                   A         B         C         D  F
2017-01-01 -1.697666  0.809194  0.066243 -0.307940  1
2017-01-02 -0.441390  2.530821 -0.085700  1.013566  2
2017-01-03 -0.276149 -0.053636 -0.291972 -0.164824  3
2017-01-04 -0.163696  0.913154 -0.758999 -0.396439  4
2017-01-05  0.693905  0.787678 -0.636466  1.503932  5
2017-01-06  0.722122  0.458462  1.416372  2.972520  6
"""
# Setting values by label:
df.at[dates[0], 'A'] = 0
# Setting values by position:
df.iat[0, 1] = 99
# Setting by assigning with a NumPy array:
df.loc[:, 'D'] = np.array([5] * len(df))
print df
"""
                   A          B         C  D  F
2017-01-01  0.000000  99.000000  0.066243  5  1
2017-01-02 -0.441390   2.530821 -0.085700  5  2
2017-01-03 -0.276149  -0.053636 -0.291972  5  3
2017-01-04 -0.163696   0.913154 -0.758999  5  4
2017-01-05  0.693905   0.787678 -0.636466  5  5
2017-01-06  0.722122   0.458462  1.416372  5  6
"""
# A where operation with setting.
df2 = df.copy()
df2[df2 > 0] = -df2
print df2
"""
                   A          B         C  D  F
2017-01-01  0.000000 -99.000000 -0.066243 -5 -1
2017-01-02 -0.441390  -2.530821 -0.085700 -5 -2
2017-01-03 -0.276149  -0.053636 -0.291972 -5 -3
2017-01-04 -0.163696  -0.913154 -0.758999 -5 -4
2017-01-05 -0.693905  -0.787678 -0.636466 -5 -5
2017-01-06 -0.722122  -0.458462 -1.416372 -5 -6
"""
print '-----------------------------------------------------------------------------'
